package com.jie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jie.entity.UserAddress;
import com.jie.service.UserAddressService;
import com.jie.utility.R;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.beans.Transient;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jie
 * @since 2024-03-15
 */
@RestController
@Transactional
@RequestMapping("/address")
public class UserAddressController {
    @Autowired
    private UserAddressService addressService;
    @GetMapping("/findById")
    public  UserAddress findById(@RequestParam("id")Integer id){
        UserAddress byId = addressService.getById(id);
        return byId;
    }

    @PostMapping("/addAddress")
    public R<String> addAddress(@RequestBody UserAddress userAddress){
       LambdaQueryWrapper<UserAddress> wrapper=new LambdaQueryWrapper<>();
       wrapper.eq(UserAddress::getUserId,userAddress.getUserId());
        List<UserAddress> list = addressService.list(wrapper);
        if(list.size()>0){
            if( StringUtils.isNotEmpty(userAddress.getIsDefault())&&userAddress.getIsDefault().equals("1")){
                LambdaQueryWrapper<UserAddress> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(UserAddress::getIsDefault,"1");
                queryWrapper.eq(UserAddress::getUserId,userAddress.getUserId());
                UserAddress one = addressService.getOne(queryWrapper);
                one.setIsDefault("0");
                addressService.updateById(one);
            }
        }else {
            userAddress.setIsDefault("1");
        }
        boolean save = addressService.save(userAddress);
        if(save){
            return R.success("添加地址成功！");
        }
        return R.error("添加地址失败！");
    }
    @GetMapping("/allAddress")
    public  R<List<UserAddress>> allAddress(UserAddress userAddress){
        LambdaQueryWrapper<UserAddress> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAddress::getUserId,userAddress.getUserId());
        List<UserAddress> list = addressService.list(queryWrapper);
        return R.success(list);
    }

    @PutMapping("/editAddress")
    public  R<String> editAddress(@RequestBody UserAddress userAddress){
        if( StringUtils.isNotEmpty(userAddress.getIsDefault())&&userAddress.getIsDefault().equals("1")){
            LambdaQueryWrapper<UserAddress> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(UserAddress::getIsDefault,"1");
            queryWrapper.eq(UserAddress::getUserId,userAddress.getUserId());
            UserAddress one = addressService.getOne(queryWrapper);
            one.setIsDefault("0");
            addressService.updateById(one);
        }
        boolean b = addressService.updateById(userAddress);
        if(b){
            return R.success("修改地址成功！");
        }
        return R.error("修改地址失败！");
    }

    @PutMapping("/editIsDefault")
    public  R<String> editIsDefault(@RequestBody UserAddress userAddress){
        LambdaQueryWrapper<UserAddress> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAddress::getIsDefault,"1");
        queryWrapper.eq(UserAddress::getUserId,userAddress.getUserId());
        UserAddress one = addressService.getOne(queryWrapper);
        one.setIsDefault("0");
        boolean b = addressService.updateById(one);
        if(b){
            boolean c = addressService.updateById(userAddress);
            if(c) return R.success("修改默认地址成功！");
        }
        return R.error("修改默认地址失败！");
    }
    @DeleteMapping("/delAddress")
    public R<String>  delAddress(@RequestParam("id") int id){
        UserAddress byId = addressService.getById(id);
        boolean b = addressService.removeById(id);
            if(b){
                if(byId.getIsDefault().equals("1")){
                LambdaQueryWrapper<UserAddress> queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(UserAddress::getUserId,byId.getUserId());
                List<UserAddress> list = addressService.list(queryWrapper);
                if(list.size()>0){
                    UserAddress address=list.get(0);
                    address.setIsDefault("1");
                    addressService.updateById(address);
                }
            }
                return R.success("删除地址成功！");
        }

        return R.error("删除地址失败！");

    }


}

